package com.yuntongxun.ecdemo.voicechat.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.yuntongxun.ecdemo.R;
import com.yuntongxun.ecdemo.voicechat.ui.adapter.FlagAdapter;
import com.yuntongxun.ecdemo.voicechat.util.CountryUtil;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import butterknife.InjectView;

/**
 * 国籍选择
 * @author li_zhuonan
 * @version 1.0
 * @since 2016/11/16
 */

public class FlagSelectActivity extends BaseChatActivity{

    List<Bitmap> bitmaps=new ArrayList<>();
    @InjectView(R.id.flag_list)
    ListView flagLv;

    public static final String FLAG_NAME ="index";
//    String[] imgs= CountryUtil.getCountryIcon(this);
    String[] flagTitle=CountryUtil.getAllCountries();

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_flag_select;
    }

    @Override
    protected void initViewsAndEvents() {
        setTitle("Flag");
        getFlagData();
        FlagAdapter adapter=new FlagAdapter(this,bitmaps,flagTitle);
        flagLv.setAdapter(adapter);

        flagLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent=new Intent();
                intent.putExtra(FLAG_NAME,flagTitle[i]);
                setResult(RESULT_OK,intent);
                finish();
            }
        });
    }

    private void getFlagData(){

        for (int i=0;i<flagTitle.length;i++){
            try {
                InputStream is=getAssets().open("flag/"+flagTitle[i]+".png");
                bitmaps.add(BitmapFactory.decodeStream(is));
                is.close();
            } catch (IOException e) {
                Log.e("----",e.toString());
            }
        }
    }


}
